require 'JournalXMLib'
require 'date'
class ElementQuery


  def initialize(j)
    @Jobject = j
  end
  
  def queryDates(datename)
    dateImages = []
    @Jobject.trips.each do |trip|
      trip.destinations.each do |destination|
        destination.photos.each do |photo|
          if datename == photo.datetaken.datetakenElement[0].to_s
            dateImages.push(photo)
          end
        end
      end
    end
    return dateImages
  end
  
  def queryCities(cityname)
    cityImages = []
    @Jobject.trips.each do |trip|
      trip.destinations.each do |destination|
        if cityname == destination.city.cityElement[0].to_s
          destination.photos.each do |photo|
            cityImages.push(photo)
          end
        end
      end
    end
    return cityImages
  end
  
  def queryCountry(countryname)
    countryImages = []
    @Jobject.trips.each do |trip|
      trip.destinations.each do |destination|
        if countryname == destination.country.countryElement[0].to_s
          destination.photos.each do |photo|
            countryImages.push(photo)
          end
        end
      end
    end
    return countryImages
  end
  
  def queryTrip(tripname)
    tripImages = []
    @Jobject.trips.each do |trip|
      if tripname == trip.name.nameElement[0].to_s
        trip.destinations.each do |destination|
          destination.photos.each do |photo|
            tripImages.push(trip)
          end
        end        
      end
    end
    return tripImages
  end
  
  def chronology
    imagesInOrder = []
    dates = []
    @Jobject.trips.each do |trip|
      trip.destinations.each do |destination|
        destination.photos.each do |photo|
          #parses the datetaken element from the current photo into a new date object
          dateElem = Date.parse(photo.datetaken.datetakenElement[0].to_s)
          x = 0
          found = false
          #handles the initial photo and date
          if dates[0] == nil
            dates[0] = dateElem
            imagesInOrder[0] = photo
          else
            until found == true
              #splits the date into pieces
              dt = dates[x].to_s
              year = dt[0..3].to_i
              month = dt[5..6].to_i
              day = dt[8..9].to_i
              #if it reaches the end of the images photo is automatically placed
              if(imagesInOrder[x] == nil)
                imagesInOrder[x] = photo
              #year comparison
              elsif ((year - imagesInOrder[x].datetaken.datetakenElement.to_s[0..3].to_i) < 0)
                y = x
                until y == 1000
                  imagesInOrder[y+1] = imagesInOrder[y]
                  y = y+1
                  end
                dates[x] = dateElem
                imagesInOrder[x] = photo
                found = true
              #if the years are equal, month comparison
              elsif(((year - imagesInOrder[x].datetaken.datetakenElement.to_s[0..3].to_i) == 0) and ((month - imagesInOrder[x].datetaken.datetakenElement.to_s[5..6].to_i) < 0))
                y = x
                until y == 1000
                  imagesInOrder[y+1] = imagesInOrder[y]
                  y = y+1
                  end
                dates[x] = dateElem
                imagesInOrder[x] = photo
                found = true
              #if both are equal, day comparison
              elsif(((year - imagesInOrder[x].datetaken.datetakenElement.to_s[0..3].to_i) == 0) and ((month - imagesInOrder[x].datetaken.datetakenElement.to_s[5..6].to_i) == 0) and ((day - imagesInOrder[x].datetaken.datetakenElement.to_s[7..9].to_i) < 1))
                y = x
                until y == 1000
                  imagesInOrder[y+1] = imagesInOrder[y]
                  y = y+1
                  end
                dates[x] = dateElem
                imagesInOrder[x] = photo
                found = true
              else
                x = x +1
              end              
            end
          end
        end
      end
    end
    return imagesInOrder
  end
  
end
